package com.bytech.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author zhao
 * @Create 2021/7/13 15:54
 */
public class DateUtils {
    public static final SimpleDateFormat DateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat TightDateTimeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    public static final SimpleDateFormat DateHourFormat = new SimpleDateFormat("yyyy-MM-dd HH");
    public static final SimpleDateFormat TightDateHourFormat = new SimpleDateFormat("yyyyMMdd HH");
    public static final SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat TightDateFormat = new SimpleDateFormat("yyyyMMdd");

    public static String second2TimeStr(long second) {
        long h = second / 3600;
        long mi = second % 3600 / 60;
        StringBuilder sb = new StringBuilder();
        if (h > 0) {
            sb.append(h + " 小时");
        }
        if (mi > 0) {
            sb.append(mi + " 分钟");
        }
        return sb.toString();
    }

    /**
     * 得到几天后的时间
     */
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * 得到day天前的时间
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }

    /**
     * 计算时间间隔
     *
     * @param endTime   结束时间
     * @param startTime 开始时间
     */
    public static Integer daysBetween(Date endTime, Date startTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startTime);
        long time1 = cal.getTimeInMillis();
        cal.setTime(endTime);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 获取指定一天的结束时间
     */
    public static Date getEndTime(Date date) {
        Calendar dateEnd = Calendar.getInstance();
        dateEnd.setTime(date);
        dateEnd.set(Calendar.HOUR_OF_DAY, 23);
        dateEnd.set(Calendar.MINUTE, 59);
        dateEnd.set(Calendar.SECOND, 59);
        return dateEnd.getTime();
    }

    /**
     * 指定一天的开始时间
     */
    public static Date getStartTime(Date date) {
        Calendar startTime = Calendar.getInstance();
        startTime.setTime(date);
        startTime.set(Calendar.HOUR_OF_DAY, 0);
        startTime.set(Calendar.MINUTE, 0);
        startTime.set(Calendar.SECOND, 0);
        return startTime.getTime();
    }

    public static Date getDateByStr(String time) {
        Date date = null;
        try {
            date = DateTimeFormat.parse(time);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String parseDate(Date date, SimpleDateFormat format) {
        if (date == null) {
            date = new Date();
        }
        return format.format(date);
    }

    public static Date strToDate(String str, SimpleDateFormat format) {
        Date date = null;
        try {
            date = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取当前系统时段名称 上午 下午 晚上
     */
    public static String getIntervalName() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        int h = c.get(Calendar.HOUR_OF_DAY);
        return h < 6 ? "晚上" : h < 12 ? "上午" : h < 18 ? "下午" : "晚上";
    }
}
